package com.sast.sql;

import java.sql.*;
import java.text.SimpleDateFormat;
/* 数据表 轮询记录
CREATE TABLE Inventory_Log (
	    id INT AUTO_INCREMENT PRIMARY KEY,
	    epc_id VARCHAR(50) NOT NULL,
	    datetime datetime DEFAULT NULL
	);
	CREATE TABLE Epc_Table (
	    epc_id VARCHAR(20) NOT NULL PRIMARY KEY,
	    sast_num VARCHAR(20) NOT NULL,
	    name VARCHAR(10) DEFAULT NULL,
	    person VARCHAR(10) DEFAULT NULL	    
	);
	
*/
public class SelectAll {
	// MySQL 8.0 以上版本 - JDBC 驱动名及数据库 URL
    static String JDBC_DRIVER = "com.mysql.cj.jdbc.Driver";  
    static final String DB_URL = "jdbc:mysql://localhost:3306/rfid_db?useSSL=false&serverTimezone=UTC";
 
 
    // 数据库的用户名与密码，需要根据自己的设置
    static final String USER = "root";
    static final String PASS = "368212";
    String write = "insert into Inventory_Log(epc_id,datetime)values('4567895','2017-03-02 15:22:22');";

	public static void main(String[] args) {
		// TODO 自动生成的方法存根
	    Connection conn = null;
	    Statement stmt = null;
	    try {
			Class.forName("com.mysql.cj.jdbc.Driver");
            // 打开连接
            System.out.println("连接数据库...");
            conn = DriverManager.getConnection(DB_URL, USER, PASS);
 
            // 执行查询
            System.out.println("查询数据库...");
            stmt = conn.createStatement();
            ResultSet rs = stmt.executeQuery("SELECT * FROM Inventory_Log");
            System.out.println(rs);
            
         // 展开结果集数据库
            while (rs.next()) {
                // 通过字段检索
                int id = rs.getInt("id");
                String name = rs.getString("epc_id");
                Timestamp ts= rs.getTimestamp("datetime");
                String datetime = ts ==null? "": new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(ts);
                // 输出数据
                System.out.print("id: " + id);
                System.out.print(", epc_id: " + name);
                System.out.print(", datetime: " + datetime);
                System.out.println();
            }
            // 完成后关闭
            rs.close();
            stmt.close();
            conn.close();
		} catch (ClassNotFoundException e) {
			// TODO 自动生成的 catch 块
			e.printStackTrace();
		}catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			
		}
	}

}

//PreparedStatement
//存储数据
//
//Date date= new Date();
//
//SimpleDateFormat format= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//从前端或者自己模拟一个日期格式，转为String即可
//
//String dateStr =format.format(date);